//
//  digitalClock.m
//  digitalClock
//
//  Created by Le Thanh Hai on 10/7/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "digitalBlackClock.h"

@implementation digitalBlackClock


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

- (UILabel *)createLabelWithBackgroundInRect:(CGRect)rect withStartText:(NSString *)startText withSize:(float)_size{	
	UILabel *label = [[UILabel alloc] initWithFrame:rect];
    //	[label setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:_size]];
    //	label.backgroundColor = [UIColor clearColor];
    //	label.textColor = [UIColor whiteColor];
    //	[self.view addSubview:label];
    //	label.text = startText;
    //	label.textAlignment = UITextAlignmentRight;
    //	label.alpha = 0.15;
	
	label = [[UILabel alloc] initWithFrame:rect];
    //	[label setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:_size]];
    [label setFont:[UIFont fontWithName:@"Arial-BoldMT" size:_size]];
	label.backgroundColor = [UIColor clearColor];
	label.textColor = [UIColor blackColor];
	[self.view addSubview:label];
	label.text = startText;
	label.textAlignment = UITextAlignmentRight;
	return label;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Create background image
    UIImageView *bgDigitalClock = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"digital_base.png"]];
    bgDigitalClock.frame = CGRectMake(0, 0, 295, 68);
    [self.view addSubview:bgDigitalClock];
    
//    UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 295, 68)];
//    bg.backgroundColor = [UIColor whiteColor];
//    [self.view addSubview:bg];
    
    int x = 60;
	int y = 0;
	labels[0] = [self createLabelWithBackgroundInRect:CGRectMake(x, y, 40, 80) withStartText:@"8" withSize:55.0];
	labels[1] = [self createLabelWithBackgroundInRect:CGRectMake(x+=35, y, 40, 80) withStartText:@"8" withSize:55.0];
	labels[2] = [self createLabelWithBackgroundInRect:CGRectMake(x+=37, y+18, 22, 35) withStartText:@":" withSize:55.0];
	labels[3] = [self createLabelWithBackgroundInRect:CGRectMake(x+=20, y, 40, 80) withStartText:@"8" withSize:55.0];
	labels[4] = [self createLabelWithBackgroundInRect:CGRectMake(x+=35, y, 40, 80) withStartText:@"8" withSize:55.0];
    
    //	labels[5] = [self createLabelWithBackgroundInRect:CGRectMake(42, y-26, 32, 80) withStartText:@"." withSize:55.0];
    //	labels[6] = [self createLabelWithBackgroundInRect:CGRectMake(39, y-12, 32, 80) withStartText:@"." withSize:55.0];
    
    
	labels[7] = [self createLabelWithBackgroundInRect:CGRectMake(x+=40, y, 30, 90) withStartText:@"8" withSize:40.0]; // label second
	labels[8] = [self createLabelWithBackgroundInRect:CGRectMake(x+=30, y, 30, 90) withStartText:@"8" withSize:40.0];
    labels[9] = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, 36, 22)];
    labels[9].backgroundColor = [UIColor clearColor];
    labels[9].font = [UIFont fontWithName:@"Arial-BoldMT" size:15];
    labels[9].textColor = [UIColor blackColor];
    labels[9].textAlignment = UITextAlignmentCenter;
    [self.view addSubview:labels[9]];
    labels[10] = [self createLabelWithBackgroundInRect:CGRectMake(12, 23, 50, 25) withStartText:@"88" withSize:16];
    labels[10].textAlignment = UITextAlignmentCenter;
    labels[11] = [self createLabelWithBackgroundInRect:CGRectMake(12, 42, 50, 22) withStartText:@"88" withSize:15];
    labels[11].textAlignment = UITextAlignmentCenter;
	
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.50];
	[UIView setAnimationDelay:0.50];
	[UIView setAnimationRepeatCount:99999999.0];
	[UIView setAnimationRepeatAutoreverses:YES];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	labels[2].alpha = 0.0;
	[UIView commitAnimations];
	
	[self onTimer];
	timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}

- (void)onTimer {
    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *now = [NSDate date];
	int dig[6];
	int hourOfDay = [[cal components:NSHourCalendarUnit fromDate:now] hour];
	int minuteOfHour = [[cal components:NSMinuteCalendarUnit fromDate:now] minute]; 
    int secondOfMinute = [[cal components:NSSecondCalendarUnit fromDate:now] second];
    int weekOfDay = [[cal components:NSWeekdayCalendarUnit fromDate:now] weekday];
    int day = [[cal components:NSDayCalendarUnit fromDate:now] day];
    int month = [[cal components:NSMonthCalendarUnit fromDate:now] month];
    int year = [[cal components:NSYearCalendarUnit fromDate:now] year];
	
    //	labels[5].alpha = hourOfDay > 11 ? 0.0 : 1.0;
    //	labels[6].alpha = hourOfDay > 11 ? 1.0 : 0.0;
	
    //	if (hourOfDay > 12)
    //		hourOfDay -= 12;
	dig[0] = hourOfDay / 10;
	dig[1] = hourOfDay % 10;
	dig[2] = minuteOfHour / 10;
	dig[3] = minuteOfHour % 10;
    dig[4] = secondOfMinute / 10;
    dig[5] = secondOfMinute % 10;
	
	labels[0].text = [NSString stringWithFormat:@"%d", dig[0]];
	labels[1].text = [NSString stringWithFormat:@"%d", dig[1]];
	labels[3].text = [NSString stringWithFormat:@"%d", dig[2]];
	labels[4].text = [NSString stringWithFormat:@"%d", dig[3]];
	labels[7].text = [NSString stringWithFormat:@"%d", dig[4]];
	labels[8].text = [NSString stringWithFormat:@"%d", dig[5]];
    labels[9].text = [NSString stringWithFormat:@"%d", year];
    labels[10].text = [NSString stringWithFormat:@"%d/%d", month, day];
    switch (weekOfDay) {
        case 1: 
            labels[11].text =  @"SUN";
            labels[11].textColor = [UIColor redColor];
            break;
        case 2: 
            labels[11].text = @"MON";
            labels[11].textColor = [UIColor blackColor];
            break;
        case 3: 
            labels[11].text = @"TUE";
            labels[11].textColor = [UIColor blackColor];
            break;
        case 4: 
            labels[11].text = @"WED";
            labels[11].textColor = [UIColor blackColor];
            break;
        case 5: 
            labels[11].text = @"THU";
            labels[11].textColor = [UIColor blackColor];
            break;
        case 6: 
            labels[11].text = @"FRI";
            labels[11].textColor = [UIColor blackColor];
            break;
        case 7: 
            labels[11].text = @"SAT";
            labels[11].textColor = [UIColor blackColor];
            break;            
        default:
            break;
    }   
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
	// Retrieve the touch point
	CGPoint pt = [[touches anyObject] locationInView:self.view];
	startLocation = pt;
	[[self.view superview] bringSubviewToFront:self.view];
    
    
    if ([[touches anyObject] tapCount] > 1) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Show" object: nil];
    }
}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{    
	CGPoint pt = [[touches anyObject] locationInView:self.view];
	CGRect frame = [self.view frame];
    float tempX = frame.origin.x + pt.x - startLocation.x;
    float tempY = frame.origin.y + pt.y - startLocation.y;
    if (tempX<0) {
        tempX = 0;
    }
    if (tempY<0) {
        tempY = 0;
    }
    NSLog(@"%f", [self.view superview].frame.size.width -self.view.frame.size.width );
    if (tempX>[self.view superview].frame.size.width -self.view.frame.size.width) {
        tempX = [self.view superview].frame.size.width -self.view.frame.size.width;
    }
    
    
    NSLog(@"%f", [self.view superview].frame.size.height -self.view.frame.size.height );
    if (tempY>[self.view superview].frame.size.height -self.view.frame.size.height) {
        tempY = [self.view superview].frame.size.height -self.view.frame.size.height;
    }
    
    frame.origin.x = tempX;
    frame.origin.y = tempY;
    
	[self.view setFrame:frame];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
